<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        div {
            background: palegreen;
            width: 200px;
            height: 200px;
            position: absolute;
        }
    </style>
</head>

<body>
    <div id="box"></div>
    <script>
        function getStyle(element, attribute) {
            var style;
            if (element.currentStyle) {
                style = element.currentStyle[attribute];
            }
            else {
                style = window.getComputedStyle(element)[attribute];
            }
            return style;
        }
        box.onmousedown = function () {
            window.onmousemove = function (e) {
                // console.log(window.getComputedStyle(box)['height'])
                var half_box_height = parseInt(parseInt(window.getComputedStyle(box)['height']) / 2);
                var half_box_width = parseInt(parseInt(window.getComputedStyle(box)['width']) / 2);
                var topMax = e.clientY - half_box_height;
                var leftMax = e.clientX - half_box_width;
                var topMin = 0;
                var leftMin = 0;
                console.log(e.clientY)
                if (e.clientY < half_box_height || e.clientX < half_box_width) {
                    if ((e.clientY < half_box_height && e.clientX < half_box_width)) {
                        box.style.top = topMin;
                        box.style.left = leftMin;
                    }

                    else if (e.clientY < half_box_height)
                        box.style.top = topMin;
                    else
                        box.style.left = leftMin;

                }
                else {
                    if (e.clientY + half_box_height >= window.innerHeight) {
                        box.style.top = parseInt(window.innerHeight) - half_box_height * 2 + 'px'
                    }
                    else
                        box.style.top = topMax + 'px';
                    if (e.clientX + half_box_width >= window.innerWidth) {
                        box.style.left = parseInt(window.innerWidth) - half_box_width * 2 + 'px'
                    }
                    else {
                        box.style.left = leftMax + 'px';
                    }
                }





            }
        }

    </script>
</body>

</html>